package com.example.hello.controller;

import com.example.hello.common.Result;
import com.example.hello.service.FileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@RestController
@CrossOrigin(
    origins = "https://usipzsopqdmx.sealoshzh.site",  // 指定具体的前端域名
    allowedHeaders = "*",
    methods = {RequestMethod.POST, RequestMethod.GET, RequestMethod.OPTIONS},
    allowCredentials = "true"
)
public class FileController {

    @Autowired
    private FileService fileService;

    /**
     * 文件上传
     * @param file 上传的文件
     * @return 文件访问路径
     */
    @PostMapping("/upload")
    public Result<String> upload(@RequestParam("file") MultipartFile file) {
        try {
            log.info("开始处理文件上传请求: {}", file.getOriginalFilename());
            
            if (file.isEmpty()) {
                return Result.error("请选择要上传的文件");
            }
            
            String fileUrl = fileService.uploadFile(file);
            log.info("文件上传成功: {}", fileUrl);
            return Result.success(fileUrl);
            
        } catch (Exception e) {
            log.error("文件上传失败: {}", e.getMessage(), e);
            return Result.error("文件上传失败");
        }
    }
} 